package util

import (
	"database/sql"
	"math"
	"regexp"
	"time"
	"unicode"
)

var NumberChinese []string

func init() {
	NumberChinese = []string{"十", "一", "二", "三", "四", "五", "六", "七", "八", "九"}
}

func GetNullString(str string) (nullStr sql.NullString) {
	if len(str) != 0 {
		nullStr.String = str
		nullStr.Valid = true
	}
	return
}

func GetBoolString(value bool, true string, false string) string {
	if value {
		return true
	}
	return false
}

func GetDateString(value time.Time, year string, month string, day string, null string) string {
	if value.IsZero() {
		return null
	}
	return value.Format("2006" + year + "01" + month + "02" + day)
}

func IntToChinese(value int) string {
	if value == 10 {
		return "十"
	}
	if value == 0 {
		return "零"
	}
	if value < 10 {
		return NumberChinese[value]
	} else if value < 20 {
		return "十" + NumberChinese[value%10]
	} else {
		shi := value / 10
		ge := value % 10
		if ge == 0 {
			return NumberChinese[shi] + "十"
		} else {
			return NumberChinese[shi] + "十" + NumberChinese[ge]
		}
	}
}

func GetStrLength(str string) float64 {
	var total float64
	reg := regexp.MustCompile("/·|，|。|《|》|‘|’|”|“|；|：|【|】|？|（|）|、/")
	for _, r := range str {
		if unicode.Is(unicode.Scripts["Han"], r) || reg.Match([]byte(string(r))) {
			total = total + 2
		} else {
			total = total + 1
		}
	}
	return math.Ceil(total)
}
